/*
 * Copyright (C) 2004, Velocityme Partnership
 * TimeActions.java
 *
 * Created on 12 November 2004, 10:27
 */

package com.velocityme.www.interfaces;

import com.velocityme.interfaces.*;
import com.velocityme.utility.TimeEntry;
import com.velocityme.www.actionforms.TimeReportActionForm;
import com.velocityme.www.dto.TimeReportDTO;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import javax.swing.tree.TreeModel;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class TimeActions extends DispatchAction {
    
    /** Creates a new instance of TimeActions */
    public TimeActions() {
    }
    
    public ActionForward showTimeReportSetup(ActionMapping mapping, 
                                             ActionForm form,
                                             HttpServletRequest request,
                                             HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("contactablePairs", NodeUtil.getAllContactablePairs(key, cs));
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        // setup default values
        Calendar now = Calendar.getInstance();
        TimeReportActionForm timeReportActionForm = new TimeReportActionForm();
        timeReportActionForm.reset(mapping, request);
        timeReportActionForm.setNodeId(nodePK.getNodeId());
        timeReportActionForm.setStartYear(now.get(Calendar.YEAR));
        timeReportActionForm.setStartMonth(now.get(Calendar.MONTH));
        timeReportActionForm.setStartDay(now.get(Calendar.DATE));
        timeReportActionForm.setStopYear(now.get(Calendar.YEAR));
        timeReportActionForm.setStopMonth(now.get(Calendar.MONTH));
        timeReportActionForm.setStopDay(now.get(Calendar.DATE));
        session.setAttribute("timeReportActionForm", timeReportActionForm);
        return mapping.findForward("ACTION_SHOW_SETUP_SUCCESS");                                            
    }
    
    public ActionForward showTimeReport(ActionMapping mapping, 
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TimeReportActionForm timeReportActionForm = (TimeReportActionForm)form;
        NodePK nodePK = new NodePK(timeReportActionForm.getNodeId());
        if (request.getParameter("btnSubmit").equals("OK")) {
            String nodePath = cs.getNodePathName(key, nodePK);
            Calendar startDate = Calendar.getInstance(); startDate.clear();
            Calendar stopDate = Calendar.getInstance(); stopDate.clear();
            startDate.set(timeReportActionForm.getStartYear(), timeReportActionForm.getStartMonth(), timeReportActionForm.getStartDay());
            stopDate.set(timeReportActionForm.getStopYear(), timeReportActionForm.getStopMonth(), timeReportActionForm.getStopDay(), 23, 59, 59);
            Map results = cs.getNodeSearchTimeLogValue(key, 
                                                       nodePK,
                                                       timeReportActionForm.getContactablePKs(),
                                                       timeReportActionForm.getIsRecursive().booleanValue(),
                                                       timeReportActionForm.getIncludeTasks().booleanValue(),
                                                       timeReportActionForm.getIncludeTypes().booleanValue(),
                                                       timeReportActionForm.getIncludeStates().booleanValue(),
                                                       timeReportActionForm.getIncludeMonths().booleanValue(),
                                                       timeReportActionForm.getIncludeWeeks().booleanValue(),
                                                       timeReportActionForm.getIncludeDays().booleanValue(),
                                                       timeReportActionForm.getIncludeEntries().booleanValue(),
                                                       startDate.getTime(),
                                                       stopDate.getTime());
            TimeReportDTO timeReportDTO = new TimeReportDTO(nodePath,
                                                            startDate.getTime(),
                                                            stopDate.getTime(),
                                                            new Date(),
                                                            (Collection)results.get("Entries"),
                                                            (Collection)results.get("Names"),
                                                            (Collection)results.get("Summaries"));
            session.setAttribute("timeReportDTO", timeReportDTO);
            return mapping.findForward("ACTION_OK");
        }
        // Cancel simply displays the original node again
        else {
            request.setAttribute("nodePK", nodePK);
            return mapping.findForward("ACTION_CANCEL");
        }
    }
    
}
